/**
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：Users接口实现                                                    
*│　作    者：NetCoreCMS                                            
*│　版    本：1.0    模板代码自动生成                                                
*│　创建时间：2019-12-25 10:52:46                             
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│　命名空间： NetCoreCMS.Repository.SqlServer                                  
*│　类    名： UsersRepository                                      
*└──────────────────────────────────────────────────────────────┘
*/
using NetCoreCMS.DbHelper;
using NetCoreCMS.IRepository;
using NetCoreCMS.Models;
using Dapper;
using Microsoft.Extensions.Options;
using System;
using System.Threading.Tasks;
using System.Text;
using System.Linq;
using System.Data;
using System.Transactions;

namespace NetCoreCMS.Repository.SqlServer
{
    public class UsersRepository:BaseRepository<Users,Int32>, IUsersRepository
    {
        public UsersRepository(IOptionsSnapshot<DbOption> options)
        {
            _dbOption =options.Get("NetCoreCMS");
            if (_dbOption == null)
            {
                throw new ArgumentNullException(nameof(DbOption));
            }
            //_dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString);
        }

        public bool DeleteAll(int id)
        {
            using (TransactionScope scope = new TransactionScope())
            {
                using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
                {
                    Models.Users model = _dbConnection.Get<Users>(id);
                    if (model == null)
                        return false;
                    //如果用户进入直销会员且激活不可以删除
                    if (model.user_id > 0 && model.is_pass == 1)
                        return false;
                    //如果用户存在下线，把所有下线提升
                    int pid = model.pid;
                    var subuser = _dbConnection.GetList<Users>("where pid=" + model.user_id).ToList();
                    if (subuser != null)
                    {
                        for (int i = 0; i < subuser.Count; i++)
                        {
                            _dbConnection.Execute("update [" + nameof(Users) + "] set pid" + pid + " where id=@id", new { id = subuser[i].id });
                        }
                    }
                    //提升结束
                    StringBuilder sql = new StringBuilder();
                    sql.AppendLine("delete from [" + nameof(User_point_log) + "]  where user_id=@id;");
                    sql.AppendLine("delete from [" + nameof(User_amount_log) + "]  where user_id=@id;");
                    sql.AppendLine("delete from [" + nameof(User_attach_log) + "]  where user_id=@id;");
                    sql.AppendLine("delete from [" + nameof(User_code) + "]  where user_id=@id;");
                    sql.AppendLine("delete from [" + nameof(User_login_log) + "]  where user_id=@id;");
                    sql.AppendLine("delete from [" + nameof(User_addr_book) + "]  where user_id=@id;");
                    sql.AppendLine("delete from [" + nameof(User_recharge) + "]  where user_id=@id;");
                    sql.AppendLine("delete from [" + nameof(Apply) + "]  where user_id=@id;");
                    sql.AppendLine("delete from [" + nameof(Bills) + "]  where user_id=@id;");
                    sql.AppendLine("delete from [" + nameof(Transfer) + "]  where from_user_id=@id or to_user_id=@id;");
                    sql.AppendLine("delete from [" + nameof(User_message) + "]  where post_user_name=@post_user_name or accept_user_name=@accept_user_name");
                    sql.AppendLine("delete from [" + nameof(Users) + "] where id=@id;");

                    var flag= _dbConnection.Execute(sql.ToString(), new { id = id, post_user_name = model.user_name, accept_user_name = model.user_name }) > 0;
                    scope.Complete();
                    return flag;
                }                
            }
        }

        public Users GetModel(string user_name, string password, int emaillogin, int mobilelogin)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from ["+nameof(Users)+"] where (user_name=@user_name");
            if (emaillogin == 1)
            {
                strSql.Append(" or email=@user_name");
            }
            if (mobilelogin == 1)
            {
                strSql.Append(" or mobile=@user_name");
            }
            strSql.Append(") and password=@password and status<3");
            return Get(strSql.ToString(), new { user_name = user_name, password = password });
        }

        public int UpdateField(int id, string strValue)
        {
            using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                return _dbConnection.Execute("update [" + nameof(Users) + "] set " + strValue + " where id=@id", new { id = id });
            }
        }
        public bool Exists(string username)
        {
            return Get("select * from ["+nameof(Users)+"] where user_name=@user_name", new { user_name = username }) != null ? true : false;
        }
        public bool Tixian(int user_id, decimal point)
        {
            using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                return _dbConnection.Execute("update [" + nameof(Users) + "] set cash_point-=" + point + " where id=" + user_id + " and cash_point>=" + point) > 0;
            }
        }

        public bool Transfer(Transfer model)
        {
            using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                return _dbConnection.Execute("update [" + nameof(Users) + "] set cash_point-=" + model.point + " where id=" + model.from_user_id + " and cash_point>=" + model.point + " and " + model.point + ">0;update [" + nameof(Users) + "] set cash_point+=" + model.point + " where id=" + model.to_user_id) > 0;
            }
        }
    }
}